package com.tim.service.Impl;

import com.tim.dao.CellDao;
import com.tim.dao.Impl.CellDaoImpl;
import com.tim.pojo.Cell;
import com.tim.pojo.DataResult;
import com.tim.service.CellService;

import java.sql.SQLException;
import java.util.List;

/**
 * 作者：Tim
 * 时间：2021/4/28 11:07
 */
public class CellServcieImpl implements CellService {
    CellDao cellDao = new CellDaoImpl();
    @Override
    public int deleteCell(String id) {
        try {
            int cellid = Integer.parseInt(id);
            return cellDao.deleteCell(cellid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteAll(String[] ids) {
        for(String id:ids){
            int count = deleteCell(id);
        }
        return ids.length;
    }

    @Override
    public int addCell(Cell cell) {
        try {
            return cellDao.addCell(cell);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public Cell selectOne(String id) {

        try {
            int cellid =Integer.parseInt(id);
            return cellDao.selectOne(cellid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateCell(Cell cell) {
        try {
            return cellDao.updateCell(cell);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Cell> searchCell(String plotName) {
        return null;
    }

    @Override
    public DataResult limit(String currentPage, String pageSize, String plotName) {
        try {
            int p = 1;
            int s =5;
            if(currentPage!=null){
                p= Integer.parseInt(currentPage);
            }
            if(pageSize!=null){
                s=Integer.parseInt(pageSize);
            }
            List<Cell> cellList = cellDao.limit(p,s,plotName);
            DataResult dataResult = new DataResult(0,"查询成功",cellList,cellDao.count(plotName));
            return dataResult;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        DataResult dataResult = new DataResult(40000,"你真棒！！！");
        return dataResult;
    }



    @Override
    public int count(String plotName) {
        try {
            return cellDao.count(plotName);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }
}
